package com.itlong.cloud.property.service.impl;

import com.itlong.cloud.POJO.DTO.property.PropertySMSAccountInfoDTO;
import com.itlong.cloud.POJO.DTO.property.PropertySMSOverviewDTO;
import com.itlong.cloud.POJO.VO.property.PropertySMSAccountInfoVO;
import com.itlong.cloud.POJO.VO.property.PropertySMSOverviewVO;
import com.itlong.cloud.property.dao.IPropertySMSOverviewDao;
import com.itlong.cloud.property.service.IPropertySMSOverviewService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <desc>
 *     短信概览处理接口实现.
 * </desc>
 *
 * @createDate 2017-10-19
 */
@Service
public class PropertySMSOverviewServiceImpl implements IPropertySMSOverviewService {

    @Autowired
    IPropertySMSOverviewDao iPropertySMSOverviewDao;

    /**
     * <desc>
     * 分页查询 短信概览
     * <desc/>
     *
     * @param pageDTO 分页查询参数
     * @return
     * @author ZiXuan
     * @createDate 2017-10-19
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertySMSOverviewVO> getPage(PropertySMSOverviewDTO pageDTO) throws Exception {
        //分页对象
        Page<PropertySMSOverviewVO> page = new Page<>();
        //将DTO转换为Map
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO, page);

        List<PropertySMSOverviewVO> dataList = iPropertySMSOverviewDao.getSimplePage(params);

        for (PropertySMSOverviewVO propertySMSOverviewVO : dataList){

            propertySMSOverviewVO.setSmsTypeName(propertySMSOverviewVO.getSMSTypeName());
        }
        //填充返回数据集
        page.setRows(dataList, iPropertySMSOverviewDao.getCount(params));
        return page;
    }

    /**
     * <desc>
     *     短信统计
     * <desc/>
     *
     * @param pageDTO   信息DTO
     * @return
     * @author ZiXuan
     * @createDate 2017-10-23
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertySMSAccountInfoVO getSMSAccountInfo(PropertySMSAccountInfoDTO pageDTO) throws Exception {
        PropertySMSAccountInfoVO data = iPropertySMSOverviewDao.getSMSAccountInfo(pageDTO.getProjectId());

        if (data == null){
            return null;
        }
        //剩余短信次数
        Integer intSurplusTotal = data.getAccountTotal() - data.getConsumeTotal();
        if (intSurplusTotal < 0){
            intSurplusTotal = 0;
        }
        data.setSurplusTotal(intSurplusTotal);

        //昨日消费计数
        Integer intYDayConsumeCount = iPropertySMSOverviewDao.getSMSYDayConsumeCount(pageDTO.getProjectId());
        if (intYDayConsumeCount == null){
            intYDayConsumeCount = 0;
        }
        data.setYdayConsumeCount(intYDayConsumeCount);
        return data;
    }
}
